//var idField;
var params;
$.zljgAddressBook = function(options) {
	this.settings = $.extend(true, {}, $.zljgAddressBook.defaults, options );
	$.sdAddressBook.settings = this.settings;
	//idField=this.settings.idFieldId;
	params={
			title:this.settings.title,
			openType:this.settings.openType,
			type:this.settings.type,
			defaultChecked:this.settings.defaultChecked,
			selectType:this.settings.selectType,
			num:this.settings.num, 
			idFieldId:this.settings.idFieldId,
			textFieldId:this.settings.textFieldId,
			includeParent:this.settings.includeParent,
			defaultParams: this.settings.defaultParams
	};
	if(this.settings.openType == 'windows'){
		this.initWindows();
	}else{
	 	this.init();	
	}
};

$.sdAddressBook.settings = function(settings){
	return settings;
}

$.zljgAddressBook.compIds = function(data){
	var ids = "";
	if (data.length == 1) {
		ids += data[0].id;
		// 下面代码会在id后拼上 !@!
		//ids += data[0].id+$.sdAddressBook.consts.separator+data[0].text;
	} else {
		for (var i = 0; i < data.length; i++) {
			if (i == data.length - 1) {
				ids += data[i].id;
				//ids += data[i].id+$.sdAddressBook.consts.separator+data[i].text;
			} else {
				ids += data[i].id + "&";
				//ids += data[i].id+$.sdAddressBook.consts.separator+data[i].text + "&";
			}
		}
	}
	return ids;
}

$.zljgAddressBook.compTexts = function(data){
	var texts = "";
	if (data.length == 1) {
		texts += data[0].text;
	} else {
		for (var i = 0; i < data.length; i++) {
			if (i == data.length - 1) {
				texts += data[i].text;
			} else {
				texts += data[i].text + "、";
			}
		}
	}
	return texts;
};
/*function getIds(){
	var idStr=null;
	if($("#"+idField).val){
		idStr=$("#"+idField).val();
	}else{
		idStr=$("#"+idField).html();
	}
	return idStr;
}*/
/*
 * 用打开窗口的方式回调的方法
 */
$.zljgAddressBook.winCallback = function(msg){
 	if(msg.hasOk){
  		msg.ids = $.zljgAddressBook.compIds(msg.data);
		msg.texts = $.zljgAddressBook.compTexts(msg.data);
			if($("#"+$.sdAddressBook.settings.idFieldId).val){
				$("#"+$.sdAddressBook.settings.idFieldId).val(msg.ids);
			}else{
				$("#"+$.sdAddressBook.settings.idFieldId).html(msg.ids);
			}
			if($("#"+$.sdAddressBook.settings.textFieldId).val){
				$("#"+$.sdAddressBook.settings.textFieldId).val(msg.texts);
			}else{
				$("#"+$.sdAddressBook.settings.textFieldId).html(msg.texts);
			}
			if($.sdAddressBook.settings.callback){
				$.sdAddressBook.settings.callback(msg);
			}
	}
};

$.extend($.zljgAddressBook, {
	defaults:{
		title:"我的地址本",
		openType : "windows", //打开方式
		url :  baseURL+"/bcaddressbook/addressbook/frame.do",
		type : 0,//显示的面板,0表示所有面板,1表示产品国标目录，2表示抽查产品目录
		defaultChecked : 0,//默认展开的面板,1表示产品国标目录，2表示抽查产品目录
		selectType : 0,//确定数据范围
		num: 0, //小于等于0代表不限制选定对象个数
		idFieldId : "",//回显数据的input或div的id
		textFieldId : "",//回显数据名称的input或div的id
		includeParent : 0,//点击节点时，是否包含父节点在待选框 1包含 0不包含 默认包含
		defaultParams: null,
		callback : function(msg){
			
		}
	},
	prototype: {
		init: function() {
			var idFieldId=this.settings.idFieldId;
			var textFieldId=this.settings.textFieldId;
			var callback=this.settings.callback;
			BootstrapDialog.show({
				title: this.settings.title,
				size: BootstrapDialog.SIZE_WIDER,
			    message: function(dialog) {
			        var $message = $('<div></div>');
			        var pageToLoad = dialog.getData('pageToLoad');
			        $message.load(pageToLoad);
			        return $message;
			    },
			    data: {
			        'pageToLoad': this.settings.url+(this.settings.url.indexOf("?")>0 ? "&" : "?")+$.param(params),
			    },
		        buttons: [{
		            label: '提交',
		            cssClass: 'btn-primary',
		            action: function(dialog){
		            	confirm(function(msg){
		            		if(msg.hasOk){
		            			dialog.close();
		            			msg.ids = $.zljgAddressBook.compIds(msg.data);
		            			msg.texts = $.zljgAddressBook.compTexts(msg.data);
		            				if($("#"+idFieldId).val){
		            					$("#"+idFieldId).val(msg.ids);
		            				}else{
		            					$("#"+idFieldId).html(msg.ids);
		            				}
		            				if($("#"+textFieldId).val){
		            					$("#"+textFieldId).val(msg.texts);
		            				}else{
		            					$("#"+textFieldId).html(msg.texts);
		            				}
		            			if(callback){
		            				callback(msg);
		            			}
		            		}
		            	});
		            }
		        }, {
		            label: '取消',
		            cssClass: 'btn-orange',
		            action: function(dialogItself){
		                dialogItself.close();
		            }
		        }]
			});
		},
		initWindows:function(){
			window.open(this.settings.url+(this.settings.url.indexOf("?")>0 ? "&" : "?")+$.param(params), "_blank");
		}
	}
});
(function($){
	$.extend($.fn, {
		zljgAddressBook : function(options){
			return new $.zljgAddressBook(options);
		}
	})
})(jQuery);

